package com.guoguo.api;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.guoguo.base.BaseController;
import com.guoguo.base.TaskResource;
import com.guoguo.entity.Users;
import com.guoguo.enums.TaskEnum;
import com.guoguo.enums.UsersEnum;
import com.guoguo.service.IUsersService;
import com.guoguo.service.UserTaskInfoService;

@RestController
@RequestMapping("/api/task")
public class TaskController extends BaseController{
	
	private final Logger logger = LoggerFactory.getLogger(InviteController.class);
	
	@Autowired
	private UserTaskInfoService userTaskInfoService;
	
	@Autowired
    private IUsersService usersService;
	
	/**
	 * 礼包中心首页显示
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "/giftCentre", method = RequestMethod.POST)
    public JSONObject giftCentre(HttpServletRequest request, @RequestParam String userId){
		logger.info("into TaskController giftCentre method");
		Map<String, Object> data = new HashMap<String, Object>();
		Users users = new Users();
		try {
			if (StringUtils.isBlank(userId)) {
	        	return returnResult(UsersEnum.LOGIN_EMPTY_ERROR.getId(), "", UsersEnum.LOGIN_EMPTY_ERROR.getValue());//用户id为空
	        }else {
	        	users = usersService.selectByPrimaryKey(userId);
	        	if(users == null){
	        		return returnResult(UsersEnum.LOGIN_NOT_USER.getId(), "", UsersEnum.LOGIN_NOT_USER.getValue());//用户不存在
	        	}
	        }
			data = userTaskInfoService.giftCentre(userId);
			return returnResult(TaskEnum.SUCCESS.getId(), data, TaskEnum.SUCCESS.getValue());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return returnResult(TaskEnum.ERROR.getId(), "false", TaskEnum.ERROR.getValue());
		}
    }
	
	
	/**
	 * 领取奖励任务
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "/reward", method = RequestMethod.POST)
    public JSONObject reward(HttpServletRequest request, @RequestParam String userId, @RequestParam Integer giftType,@RequestParam Integer requireType){
		logger.info("into TaskController reward method");
		Users users = new Users();
		String isSuccess = null;
		try {
			if (StringUtils.isBlank(userId)) {
	        	return returnResult(UsersEnum.LOGIN_EMPTY_ERROR.getId(), "", UsersEnum.LOGIN_EMPTY_ERROR.getValue());//用户id为空
	        }else {
	        	users = usersService.selectByPrimaryKey(userId);
	        	if(users == null){
	        		return returnResult(UsersEnum.LOGIN_NOT_USER.getId(), "", UsersEnum.LOGIN_NOT_USER.getValue());//用户不存在
	        	}
	        }
			isSuccess = userTaskInfoService.reward(userId, giftType, requireType);
			if(StringUtils.isBlank(isSuccess)){
				return returnResult(TaskEnum.RECEIVE_ERROR.getId(), "false", TaskEnum.RECEIVE_ERROR.getValue());
		    }else if(isSuccess.equals("200")){
		    	if(giftType == TaskResource.GIFT_SIGN_TYPE){
		    		userTaskInfoService.selectWeekCount(userId);
		    	}
				return returnResult(TaskEnum.RECEIVE_SUCCESS.getId(), "true", TaskEnum.RECEIVE_SUCCESS.getValue());
			}else if(isSuccess.equals("202")){
				return returnResult(TaskEnum.GIFT_ERROR.getId(), "true", TaskEnum.GIFT_ERROR.getValue());
			}else if(isSuccess.equals("203")){
				return returnResult(TaskEnum.REPEAT_RECEIVE.getId(), "true", TaskEnum.REPEAT_RECEIVE.getValue());
			}else if(isSuccess.equals("205")){
				return returnResult(TaskEnum.INCONFORMITY_ERROR.getId(), "true", TaskEnum.INCONFORMITY_ERROR.getValue());
			}else{
				return returnResult(TaskEnum.RECEIVE_ERROR.getId(), "false", TaskEnum.RECEIVE_ERROR.getValue());
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return returnResult(TaskEnum.ERROR.getId(), "false", TaskEnum.ERROR.getValue());
		}
    }
	
	

}
